/*
 * TableroSDL.cpp
 *
 *  Created on: 23/10/2013
 *      Author: fernando
 */

#include "TableroSDL.h"
#include "resize++.h"
#include <iostream>
#include <stdlib.h>

using std::string;
using std::vector;
using std::cout;
using std::endl;

const int SIZECELL = 64;


TableroSDL::TableroSDL(int columns, int rows) {
	columnas = columns;
	filas = rows;
	ancho = columns * 64 + SIZEEXTRA;
	alto = rows * 64;
	fondo = NULL;
}

TableroSDL::~TableroSDL() {
	clean_up(fondo);
	for (vector<CeldaSDL*>::iterator It = celdas.begin(); It != celdas.end(); ++It){
		delete (*It);
	}
}

bool TableroSDL::setCeldas(vector<CeldaSDL*> v_celdas){
	if (v_celdas.size() == columnas*filas){
		celdas = v_celdas;
		return true;
	}
	return false;
}


int TableroSDL::dibujar(){
	SDL::apply_surface(0, 0, fondo, screen, NULL);
	for (vector<CeldaSDL*>::iterator It = celdas.begin(); It != celdas.end(); ++It){
		(*It)->dibujar(screen);
	}
	for (vector<Puntaje*>::iterator It2 = puntajes.begin(); It2 != puntajes.end(); ++It2){
		(*It2)->dibujar(screen);
	}
	//Apply the images to the screen
	SDL_Flip(screen);
	return 0;
}

int TableroSDL::dibujarSinCeldas(){
	for (vector<CeldaSDL*>::iterator It = celdas.begin(); It != celdas.end(); ++It){
		(*It)->dibujarGolosina(screen);
	}
	//Apply the images to the screen
	SDL_Flip(screen);
//	clean_up(background);
	return 0;
}


Golosina* TableroSDL::getGolosinaConPosicion(unsigned int x, unsigned int y){
	Golosina* golosina = NULL;
	CeldaSDL* cell = getCeldaFromPos(x,y);
	if (cell != NULL){
		golosina = cell->getGolosina();
	}
	return golosina;
}


void TableroSDL::intercambiar(CeldaSDL* celdaOrigen, CeldaSDL* celdaDestino){
	Golosina* golosinaPuente;
	Golosina* golosinaDestino;
	golosinaPuente = celdaOrigen->getGolosina();
	golosinaDestino = celdaDestino->getGolosina();
	celdaOrigen->setGolosina(golosinaDestino);
	golosinaDestino->setPosicion(celdaOrigen->getPosX(), celdaOrigen->getPosY());
	celdaDestino->setGolosina(golosinaPuente);
	golosinaPuente->setPosicion(celdaDestino->getPosX(), celdaDestino->getPosY());
}

CeldaSDL* TableroSDL::getCeldaFromPos(unsigned int x, unsigned int y){
	CeldaSDL* cell = NULL;
	int nCol, nFil, nCel;
	if (x < ancho && x >= 0 && y < alto && y >= 0){
		nCol = (x/64);
		nFil = (y/64);
		nCel = nCol*filas + nFil;
		cell = celdas[nCel];
	}
	return cell;
}

CeldaSDL* TableroSDL::getCelda(unsigned int pos){
	return celdas.at(pos);
}


void TableroSDL::setFondo(string fileName){
	fondo  = load_image(fileName);
	fondo = SDL_Resize(fondo, ancho, alto, true, 4);
}

unsigned int TableroSDL::getPosCorner(unsigned int pos){
	int aux = (pos/SIZECELL)*SIZECELL;
	if(aux == 0){
		aux = 1;
	}
	return aux;
}

unsigned int TableroSDL::getTotalCeldas(){
	return (filas*columnas);
}

unsigned int TableroSDL::getNumeroColumnas(){
	return columnas;
}

unsigned int TableroSDL::getNumeroFilas(){
	return filas;
}

unsigned int TableroSDL::getAncho(){
	return ancho;
}

void TableroSDL::sumarPuntos(unsigned int nJugador, long puntos){
	puntajes.at(nJugador)->sumarPuntos(puntos);
}

void TableroSDL::createPuntajes(unsigned int jugadores, unsigned int x, vector<string> nombres){
	Puntaje* puntaje;
	for (unsigned int i = 0; i < jugadores; i++){
		puntaje = new Puntaje(nombres.at(i));
		puntaje->setPosicion(x, i*60 + 10);
		puntajes.push_back(puntaje);

	}
}



//int TableroSDL::dibujar(){
//	vector<SDL_Surface*> casillas;
//	SDL_Surface *casillaVerde = NULL;
//	SDL_Surface* casillaVioleta = NULL;
//	SDL_Surface* casillaRoja = NULL;
//	SDL_Surface* casillaAmarilla = NULL;
//	SDL_Surface* barritaAmarillaH = NULL;
//	SDL_Surface* barritaAmarillaV = NULL;
//	SDL_Surface* barritaAzulH = NULL;
//	SDL_Surface* barritaAzulV = NULL;
//	SDL_Surface* barritaVerdeH = NULL;
//	SDL_Surface* barritaVerdeV = NULL;
//	SDL_Surface* barritaRojaH = NULL;
//	SDL_Surface* barritaRojaV = NULL;
//	SDL_Surface* barritaVioletaH = NULL;
//	SDL_Surface* barritaVioletaV = NULL;
//	SDL_Surface* casillaAzul =  NULL;
//	SDL_Surface* goldenStar =  NULL;
//	SDL_Surface* background = NULL;
//	SDL_Surface* background2 = NULL;
//	SDL_Event event;
//	int counter = 0;
//	int iSecret;
//	/* initialize random seed: */
//	srand (time(NULL));
//	bool quit = false;
//	if(init("PruebaTableroSDL") == false){
//		return 1;
//	}
//	casillaVerde = load_image("golosinaVerde.png");
//	casillas.push_back(casillaVerde);
//	casillaVioleta = load_image("golosinaVioleta.png");
//	casillas.push_back(casillaVioleta);
//	casillaRoja = load_image("golosinaRoja.png");
//	casillas.push_back(casillaRoja);
//	casillaAmarilla = load_image("golosinaAmarilla.png");
//	casillas.push_back(casillaAmarilla);
//	casillaAzul = load_image("golosinaAzul.png");
//	casillas.push_back(casillaAzul);
//	background = load_image("Celda.png");
//	background2 = load_image("CeldaNoDisponible.png");
//	barritaAmarillaH = load_image("BarritaAmaH.png");
//	casillas.push_back(barritaAmarillaH);
//	barritaAmarillaV = load_image("BarritaAmaV.png");
//	casillas.push_back(barritaAmarillaV);
//	barritaAzulH = load_image("BarritaAzuH.png");
//	casillas.push_back(barritaAzulH);
//	barritaAzulV = load_image("BarritaAzuV.png");
//	casillas.push_back(barritaAzulV);
//	barritaVerdeH = load_image("BarritaVerH.png");
//	casillas.push_back(barritaVerdeH);
//	barritaVerdeV = load_image("BarritaVerV.png");
//	casillas.push_back(barritaVerdeV);
//	barritaRojaH = load_image("BarritaRojH.png");
//	casillas.push_back(barritaRojaH);
//	barritaRojaV = load_image("BarritaRojV.png");
//	casillas.push_back(barritaRojaV);
//	barritaVioletaH = load_image("BarritaVioH.png");
//	casillas.push_back(barritaVioletaH);
//	barritaVioletaV = load_image("BarritaVioV.png");
//	casillas.push_back(barritaVioletaV);
//	goldenStar = load_image("goldenStar.png");
//	casillas.push_back(goldenStar);
//	//Tamaño del tablero (64*columnas)Ancho x (64*filas) Alto
//	for (unsigned int i = 0; i < columnas; i++){
//		for (unsigned int j = 0; j < filas; j++){
//			unsigned int x = i*64;
//			unsigned int y = j*64;
//			iSecret = rand() % 16;
//			if (iSecret%11 == 0){
//				apply_surface(x, y, background2, screen, NULL);
//			}else{
//				apply_surface(x, y, background, screen, NULL);
//
//				//Pone como transparente el color negro
//				SDL_SetColorKey(casillas[iSecret], SDL_SRCCOLORKEY, SDL_MapRGB(casillas[iSecret]->format, 0, 0, 0));
//				apply_surface(x, y, casillas[iSecret], screen, NULL);
//				counter++;
//			}
//		}
//	}
//	//Apply the images to the screen
//	SDL_Flip(screen);
//	//While the user hasn't quit
//	while(quit == false){
//		//While there's an event to handle
//		//SDL_WaitEvent() and SDL_PeepEvents() can be used too
//		while(SDL_PollEvent(&event)){
//			//If the user has Xed out the window
//			if(event.type == SDL_QUIT){
//				//Quit the program
//				quit = true;
//			}
//		}
//	}
//	//Free the surface and quit SDL
//	for (vector<SDL_Surface*>::iterator It = casillas.begin();
//			It != casillas.end(); ++It){
//		clean_up(*It);
//	}
//	clean_up(background);
//	return 0;
//}
